package com.tebiecloud.mpweixin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUser;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserExpand;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserWallet;
import com.tebiecloud.mpweixin.client.model.vo.PersonalInfoVO;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserExpandMapper;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserMapper;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserWalletMapper;
import com.tebiecloud.mpweixin.server.service.MpweixinUserExpandService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.mpweixin.server.service.MpweixinUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户拓展表 服务实现类
 * @author tebie
 * @date 2019-09-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MpweixinUserExpandServiceImpl extends BaseServiceImpl<MpweixinUserExpandMapper, MpweixinUserExpand> implements MpweixinUserExpandService {

    @Autowired
    MpweixinUserMapper mpweixinUserMapper;

    @Autowired
    MpweixinUserExpandMapper mpweixinUserExpandMapper;

    @Autowired
    MpweixinUserWalletService mpweixinUserWalletService;

    @Override
    public PersonalInfoVO infoPersonal(long userId,String openid) {
        PersonalInfoVO personalInfoVO = new PersonalInfoVO();
        MpweixinUserExpand mpweixinUserExpand = this.createOrFind(userId,openid);
        if(mpweixinUserExpand != null){
            personalInfoVO.setHeadimgurl(mpweixinUserExpand.getHeadimgurl());
            personalInfoVO.setNickname(mpweixinUserExpand.getNickname());
            personalInfoVO.setPhone(mpweixinUserExpand.getPhone());
            //查询账户资金
            MpweixinUserWallet mpweixinUserWallet = mpweixinUserWalletService.createOrFind(userId);
            if (mpweixinUserWallet != null) {
                personalInfoVO.setBalance(mpweixinUserWallet.getBalance());
                personalInfoVO.setMggCoin(mpweixinUserWallet.getMgg());
            }
            //优惠券暂未开发
            personalInfoVO.setCouponAmount(0);
        }
        return personalInfoVO;
    }

    @Override
    public MpweixinUserExpand information(long userId,String openid) {
        return this.createOrFind(userId,openid);
    }

    @Override
    public MpweixinUserExpand createOrFind(long userId,String openid) {
        //先查询
        QueryWrapper<MpweixinUserExpand> mpweixinUserExpandQueryWrapper = new QueryWrapper<>();
        mpweixinUserExpandQueryWrapper.eq("user_id",userId);
        MpweixinUserExpand mpweixinUserExpand = mpweixinUserExpandMapper.selectOne(mpweixinUserExpandQueryWrapper);
        if(mpweixinUserExpand==null){
            //获取数据同步
            if(StringUtils.isNotEmpty(openid)){
                QueryWrapper<MpweixinUser> mpweixinUserQueryWrapper = new QueryWrapper<>();
                mpweixinUserQueryWrapper.eq("openid",openid);
                MpweixinUser mpweixinUser = mpweixinUserMapper.selectOne(mpweixinUserQueryWrapper);
                if(mpweixinUser!=null){
                    mpweixinUserExpand = new MpweixinUserExpand();
                    mpweixinUserExpand.setHeadimgurl(mpweixinUser.getHeadimgurl());
                    mpweixinUserExpand.setBirthday(new Date());
                    mpweixinUserExpand.setNickname(mpweixinUser.getNickname());
                    mpweixinUserExpand.setSex(mpweixinUser.getSex());
                    mpweixinUserExpand.setUserId(userId);
                    mpweixinUserExpandMapper.insert(mpweixinUserExpand);
                }
            }
        }
        return mpweixinUserExpand;
    }
}
